'use strict';

const Service = require('egg').Service;

const svgCaptcha = require('svg-captcha'); //验证码

class UserService extends Service {
  async selectAccount(account) {
    try {
      const user = await this.app.mysql.get('users', { account: account });
      return user;
    } catch (err) {
      return 'err';
    }
  }


  async captcha() {
    // 第三方插件，
    const captcha = svgCaptcha.create({
      // size: 4,                 	// 长度(显示几个字符)
      // fontSize: 50,           	// 字体大小
      // width: 100,              	// 宽度
      // height: 58,            		// 高度
      background: '#c9cdcd',   	// 背景颜色
      //线条数
      noise: Math.floor(Math.random() * 5),
      color: true,
      fontSize: 55,
      width: 90,
      height: 38,

    });
    // 将验证码text文本保存到全局session中
    this.ctx.session.code = captcha.text;
    console.log(this.ctx.session.code)
    //验证码存redis
    // await m.app.redis.get('db1').set(m.ctx.request.ip,captcha.text,"EX",60)  //ex是写死的，60是秒
    return captcha;
  }

  async verify(data) {
    try {
      const user = await this.app.mysql.get('users', { account: data.account, email: data.email });
      console.log(user, 'user');
      return user;
    } catch (err) {
      return 'err';
    }
  }

  async resetPassword(data) {
    try {
      const user = await this.app.mysql.update('users', { password: data.password, id: data.id });
      console.log(user, 'user');
      return user;
    } catch (err) {
      return 'err';
    }
  }




}

module.exports = UserService;
